<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="故障模式">
          <el-select
            v-model="searchParam.faultModeId"
            placeholder="请选择故障模式"
          >
            <el-option
              v-for="item in faultModeList"
              :key="item.id"
              :label="item.faultMode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="table.modeAsset.getDataFunc"
            ><i-ep-search />搜索</el-button
          >
          <!--          <el-button @click="reset"><i-ep-refresh />重置</el-button>-->
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <div
        style="
          display: flex;
          flex-direction: row;
          justify-content: space-between;
        "
      >
        <div style="width: 25%">
          <el-form :model="searchParam" :inline="true">
            <div
              style="
                display: flex;
                justify-content: space-between;
                margin-bottom: 20px;
              "
            >
              <el-input
                v-model="searchParam[table.modeAsset.name]"
                :placeholder="`请输入${table.modeAsset.title}`"
                clearable
                style="width: 50%"
              />
              <el-button type="primary" @click="table.modeAsset.getDataFunc"
                ><i-ep-search />搜索</el-button
              >
              <el-button
                type="success"
                plain
                @click="openDialog(table.modeAsset, searchParam.faultModeId)"
              >
                <i-ep-plus />
                新增
              </el-button>
            </div>
          </el-form>
          <el-table
            ref="dataTableRef"
            :data="table.modeAsset.data"
            highlight-current-row
            border
            style="display: inline-block"
            @row-click="table.modeAsset.handleChange"
          >
            <el-table-column
              label="设备种类"
              prop="assetTypeName"
              min-width="100"
              show-overflow-tooltip
            />
            <el-table-column
              fixed="right"
              label="操作"
              width="100"
              align="center"
            >
              <template #default="scope">
                <el-button
                  type="danger"
                  size="small"
                  link
                  @click="table.modeAsset.delete(scope.row)"
                  ><i-ep-delete />
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div
          style="width: 25%; padding: 0 0 0 20px"
          v-for="(item, index) in Object.values(table).filter(
            (t) => t.name !== 'modeAsset'
          )"
          :key="index"
        >
          <el-form :model="searchParam" :inline="true">
            <div
              style="
                display: flex;
                justify-content: space-between;
                margin-bottom: 20px;
              "
            >
              <el-input
                v-model="searchParam[item.name]"
                :placeholder="`请输入${item.title}`"
                clearable
                style="width: 50%"
              />
              <el-button type="primary" @click="item.getDataFunc"
                ><i-ep-search />搜索</el-button
              >
              <el-button
                type="success"
                plain
                @click="openDialog(item, table[item.last].selected)"
                ><i-ep-plus />新增</el-button
              >
            </div>
          </el-form>
          <el-table
            ref="dataTableRef"
            :data="item.data"
            highlight-current-row
            border
            style="display: inline-block"
            @row-click="item.handleChange"
          >
            <el-table-column
              label="序号"
              type="index"
              prop="index"
              min-width="40"
              show-overflow-tooltip
              align="center"
            />
            <el-table-column
              :label="item.title"
              :prop="item.name"
              min-width="100"
              :show-overflow-tooltip="item.name !== 'faultHandleMeasure'"
            />
            <el-table-column fixed="right" label="操作" width="140">
              <template #default="scope">
                <el-button
                  type="primary"
                  size="small"
                  link
                  @click="
                    openDialog(item, table[item.last].selected, scope.row)
                  "
                  ><i-ep-edit />
                  修改
                </el-button>
                <el-button
                  type="danger"
                  size="small"
                  link
                  @click="item.delete(scope.row)"
                  ><i-ep-delete />
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-card>

    <modeAssetDialog :assetKind="props.assetKind" />
    <addOrUpdate />
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import useEvent from "@/utils/useEvent";

defineOptions({
  name: "EAMFAULTSYSTEMMANAGE",
  inheritAttrs: false,
});

const props = defineProps({
  assetKind: {
    type: Number,
    default: 1,
  },
});

const faultModeList = ref([]);

const baseFormData = [
  {
    prop: "assetKind",
    hidden: true,
    default: props.assetKind,
  },
  {
    label: "备注",
    prop: "remarks",
    type: "textarea",
    span: 12,
  },
  {
    label: "是否启用",
    prop: "enabled",
    type: "radio",
    span: 12,
    default: 1,
  },
];

const table = ref({
  modeAsset: {
    name: "modeAsset",
    openEvent: "modeAsset",
    title: "设备种类",
    selected: undefined,
    data: [],
    path: "eamfaultmodeasset",
    clearObj: [
      "modeAsset",
      "faultPhenomenon",
      "faultReason",
      "faultHandleMeasure",
    ],
    callbackEmit: "closeModeAssetDialog",
    next: "faultPhenomenon",
    getDataFunc: async () => {
      await getList(table.value.modeAsset);
    },
    delete: (row) => {
      deleteRow(row, table.value.modeAsset);
    },
    handleChange: (modeAsset) => {
      handleChange(modeAsset, table.value.modeAsset);
    },
  },
  faultPhenomenon: {
    name: "faultPhenomenon",
    title: "故障现象",
    openEvent: "addOrUpdate",
    selected: undefined,
    data: [],
    formData: [
      {
        label: "故障模式",
        prop: "faultModeId",
        type: "select",
        disabled: true,
        span: 12,
        options: {
          data: faultModeList,
          label: "faultMode",
          value: "id",
        },
        default: computed(() => searchParam.faultModeId),
        rules: [{ required: true, message: "请输入故障现象", trigger: "blur" }],
      },
      {
        label: "资产类型",
        prop: "assetTypeId",
        type: "select",
        disabled: true,
        span: 12,
        options: {
          data: computed(() => table.value.modeAsset.data),
          label: "assetTypeName",
          value: "assetTypeId",
        },
        default: computed(() => table.value.modeAsset.selected.assetTypeId),
        rules: [
          { required: true, message: "请输入故障现象描述", trigger: "blur" },
        ],
      },
      {
        label: "故障现象",
        prop: "faultPhenomenon",
        type: "input",
        span: 24,
        rules: [{ required: true, message: "请输入故障现象", trigger: "blur" }],
      },
    ].concat(baseFormData),
    callbackEmit: "phenomenonCallback",
    path: "eamfaultphenomenon",
    clearObj: ["faultPhenomenon", "faultReason", "faultHandleMeasure"],
    getDataFunc: async () => {
      await getList(table.value.faultPhenomenon);
    },
    last: "modeAsset",
    next: "faultReason",
    delete: (row) => {
      deleteRow(row, table.value.faultPhenomenon);
    },
    handleChange: (faultPhenomenon) => {
      handleChange(faultPhenomenon, table.value.faultPhenomenon);
    },
  },
  faultReason: {
    name: "faultReason",
    title: "故障原因",
    openEvent: "addOrUpdate",
    selected: undefined,
    data: [],
    formData: [
      {
        label: "故障模式",
        prop: "faultModeId",
        type: "select",
        disabled: true,
        span: 12,
        options: {
          data: faultModeList,
          label: "faultMode",
          value: "id",
        },
        default: computed(() => searchParam.faultModeId),
        rules: [{ required: true, message: "请输入故障现象", trigger: "blur" }],
      },
      {
        label: "资产类型",
        prop: "assetTypeId",
        type: "select",
        disabled: true,
        span: 12,
        options: {
          data: computed(() => table.value.modeAsset.data),
          label: "assetTypeName",
          value: "assetTypeId",
        },
        default: computed(() => table.value.modeAsset.selected.assetTypeId),
        rules: [
          { required: true, message: "请输入故障现象描述", trigger: "blur" },
        ],
      },
      {
        label: "故障现象",
        prop: "faultPhenomenonId",
        type: "select",
        span: 24,
        disabled: true,
        options: {
          data: computed(() => table.value.faultPhenomenon.data),
          label: "faultPhenomenon",
          value: "id",
        },
        default: computed(() => table.value.faultPhenomenon.selected.id),
        rules: [{ required: true, message: "请输入故障现象", trigger: "blur" }],
      },
      {
        label: "故障原因",
        prop: "faultReason",
        type: "input",
        span: 24,
        rules: [{ required: true, message: "请输入故障原因", trigger: "blur" }],
      },
    ].concat(baseFormData),
    path: "eamfaultreason",
    clearObj: ["faultReason", "faultHandleMeasure"],
    callbackEmit: "faultReasonCallback",
    getDataFunc: async () => {
      await getList(table.value.faultReason);
    },
    last: "faultPhenomenon",
    next: "faultHandleMeasure",
    delete: (row) => {
      deleteRow(row, table.value.faultReason);
    },
    handleChange: (faultReason) => {
      handleChange(faultReason, table.value.faultReason);
    },
  },
  faultHandleMeasure: {
    name: "faultHandleMeasure",
    title: "处理措施",
    openEvent: "addOrUpdate",
    selected: undefined,
    data: [],
    formData: [
      {
        label: "故障模式",
        prop: "faultModeId",
        type: "select",
        disabled: true,
        span: 12,
        options: {
          data: faultModeList,
          label: "faultMode",
          value: "id",
        },
        default: computed(() => searchParam.faultModeId),
        rules: [{ required: true, message: "请输入故障现象", trigger: "blur" }],
      },
      {
        label: "资产类型",
        prop: "assetTypeId",
        type: "select",
        disabled: true,
        span: 12,
        options: {
          data: computed(() => table.value.modeAsset.data),
          label: "assetTypeName",
          value: "assetTypeId",
        },
        default: computed(() => table.value.modeAsset.selected.assetTypeId),
        rules: [
          { required: true, message: "请输入故障现象描述", trigger: "blur" },
        ],
      },
      {
        label: "故障现象",
        prop: "phenomenonId",
        type: "select",
        span: 24,
        disabled: true,
        options: {
          data: computed(() => table.value.faultPhenomenon.data),
          label: "faultPhenomenon",
          value: "id",
        },
        default: computed(() => table.value.faultPhenomenon.selected.id),
        rules: [{ required: true, message: "请输入故障现象", trigger: "blur" }],
      },
      {
        label: "故障原因",
        prop: "faultReasonId",
        type: "select",
        span: 24,
        disabled: true,
        options: {
          data: computed(() => table.value.faultReason.data),
          label: "faultReason",
          value: "id",
        },
        default: computed(() => table.value.faultReason.selected.id),
        rules: [{ required: true, message: "请输入故障原因", trigger: "blur" }],
      },
      {
        label: "处理措施",
        prop: "faultHandleMeasure",
        type: "input",
        span: 24,
        rules: [{ required: true, message: "请输入处理措施", trigger: "blur" }],
      },
    ].concat(baseFormData),
    path: "eamfaulthandlemeasure",
    clearObj: ["faultHandleMeasure"],
    callbackEmit: "faultHandleMeasureCallback",
    getDataFunc: async () => {
      await getList(table.value.faultHandleMeasure);
    },
    last: "faultReason",
    delete: (row) => {
      deleteRow(row, table.value.faultHandleMeasure);
    },
  },
});

const { eventBusOn, eventBusEmit } = useEvent();

const searchParam = reactive({
  faultModeId: "",
  modeAsset: "",
  faultPhenomenon: "",
  faultReason: "",
  faultHandleMeasure: "",
});

const getFaultModeList = async () => {
  const { data } = await request({
    url: "/eam/eamfaultmode/getAvailableList",
    method: "post",
    data: { assetKind: props.assetKind },
  });
  faultModeList.value = data;
};

const clearType = (objName) => {
  table.value[objName].selected = undefined;
  table.value[objName].data = [];
};

const getList = async (obj) => {
  const params = reactive({});
  const {
    faultPhenomenon,
    faultReason,
    faultHandleMeasure,
    modeAsset,
    faultModeId,
  } = searchParam;
  if (obj.last !== undefined) {
    const selected = table.value[obj.last].selected;
    if (selected === undefined) return;
    const { assetTypeId, faultModeId, faultPhenomenonId, faultReasonId } =
      selected;
    Object.assign(params, {
      assetTypeId,
      faultModeId,
      faultPhenomenonId,
      faultReasonId,
      faultPhenomenon,
      faultReason,
      faultHandleMeasure,
    });
    params[`${obj.last}Id`] = selected.id;
  } else {
    Object.assign(params, { faultModeId, assetTypeName: modeAsset });
  }
  const { data } = await request({
    url: `/eam/${obj.path}/getList`,
    method: "post",
    data: params,
  });
  obj.clearObj.forEach(clearType);
  table.value[obj.name].data = data;
};

const handleChange = (selected, obj) => {
  if (selected.id === obj.selected?.id) return;
  obj.selected = selected;
  table.value[obj.next].getDataFunc();
};

onMounted(async () => {
  await init();
  Object.values(table.value).forEach((item) => {
    eventBusOn(item.callbackEmit, async () => {
      await item.getDataFunc();
    });
  });
});

const openDialog = (obj, param, row) => {
  if (param === undefined) return;
  eventBusEmit(obj.openEvent, { obj, param, row });
};
const deleteRow = async (row, { path, getDataFunc }) => {
  ElMessageBox.confirm("确认删除该条数据?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await request({
      url: `/eam/${path}/deleteItem/${row.id}`,
      method: "post",
    }).then(() => {
      ElMessage.success("删除成功！");
      getDataFunc();
    });
  });
};

const init = async () => {
  await getFaultModeList();
  searchParam.faultModeId = faultModeList.value[0]?.id;
  await table.value.modeAsset.getDataFunc();
};
</script>
